package com.aopeng.controller;

import com.aopeng.entity.AopengSeo;
import com.aopeng.entity.AopengWebsitemsg;
import com.aopeng.mapper.AopengSeoMapper;
import com.aopeng.mapper.AopengWebsitemsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 谢辉
 * @description SEO网站优化控制器
 * @data 2020/7/12
 */
@Controller
@RequestMapping("/seo")
public class AopengSeoController {

    @Autowired
    private AopengSeoMapper seoMapper;

    @Autowired
    private AopengWebsitemsgMapper websitemsgMapper;

    /**
    * 返回到seo表单页，如果对象不为空，则查出来放在输入框内，如果无则不做操作返回到页面
    * @author 谢辉
    * @return websites-optimize.html  返回到seo表单页
    * @date      2020/7/13 5:34
    */
    @GetMapping
    public String websitesOptimizePage(Model model){
        AopengSeo Seo = seoMapper.selectOne(null);
        if (Seo!=null){
            model.addAttribute("seo",Seo);
            return "admin/websites-optimize";
        }
        return "admin/websites-optimize";
    }

    /**
    * 如果对象不为空则做修改操作，如果无则做插入操作
    * @author   谢辉
    * @return   redirect:/seo 重定向到上一个请求中
    * @date      2020/7/13 5:36
    */
    @RequestMapping(value = "/webmsg",method = RequestMethod.POST)
    public String formSubmit(@RequestParam("inputTitle")String inputTitle,
                             @RequestParam("inputDescription")String inputDescription,
                             @RequestParam("inputKeywords")String inputKeywords){

        AopengSeo Seo = seoMapper.selectOne(null);
        if (Seo!=null){
            AopengSeo aopengSeo = new AopengSeo(Seo.getId(),inputTitle,inputDescription,
                    inputKeywords,1L,1L,1);
            seoMapper.updateById(aopengSeo);
            return "redirect:/seo";
        }
        AopengSeo aopengSeo = new AopengSeo(inputTitle,inputDescription,inputKeywords,1L,1L,1);
        seoMapper.insert(aopengSeo);

        return "redirect:/seo";

    }


    /**
    * 方法实现说明
    * @author   谢辉
    * @return   返回到websites-message.html页面
    * @date      2020/7/13 5:39
    */
    @RequestMapping(value = "/message",method = RequestMethod.GET)
    public String webMessagePage(Model model){
        AopengWebsitemsg websitemsg = websitemsgMapper.selectOne(null);
        if (websitemsg!=null){
            model.addAttribute("msg",websitemsg);
            return "admin/websites-message";
        }
        return "admin/websites-message";
    }

    @RequestMapping(value = "/postMessage",method = RequestMethod.POST)
    public String postMessage(AopengWebsitemsg aopengWebsitemsg,Model model){
        AopengWebsitemsg websitemsg = websitemsgMapper.selectOne(null);
        //若查出的对象不为空，则代表已经存在了，进入if执行修改操作，否则执行插入操作
        if (websitemsg!=null){
            websitemsgMapper.updateById(aopengWebsitemsg);
            model.addAttribute("msg",websitemsg);
            return "admin/websites-message";
        }
        websitemsgMapper.insert(aopengWebsitemsg);
        return "admin/websites-message";
    }
}
